package cn.bornsoft.mobileServer.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

import org.apache.log4j.Logger;

import cn.bornsoft.mobileServer.logic.ServiceLogic;
import cn.bornsoft.utils.JsonDateValueProcessor;

/**
 * 接受客户端请求的Servlet
 * 
 * @author JONES
 * 
 */
public class QService extends HttpServlet {
	static Logger log = Logger.getLogger(QService.class);

	/**
	 * Constructor of the object.
	 */
	public QService() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = response.getWriter();
		String jsoncallback = request.getParameter("jsoncallback");
		String type = request.getParameter("type");
		String js = "";
		ServiceLogic qServiceLogic = new ServiceLogic();
		/**
		 * 显示某一或某些栏目的内容列表
		 */
		if (type.equals("list")) {
			String max = request.getParameter("max");
			int max1 = Integer.parseInt(max == null || "".equals(max) ? "0"
					: max);
			String page = request.getParameter("page");
			int page1 = Integer.parseInt(page == null || "".equals(page) ? "0"
					: page);
			String classid = request.getParameter("classid");
			String newsids = request.getParameter("newsids");
			if (newsids == null) {
				js = qServiceLogic.getNewsList(max1, page1, classid);
			} else {
				js = qServiceLogic.getNewsListByNewsid(max1, page1, newsids);
			}
		}
		/**
		 * 显示某条内容
		 */
		else if (type.equals("view")) {
			String newsid = request.getParameter("newsid");
			js = qServiceLogic.getNewsContent(newsid);
		}
		/**
		 * 显示栏目列表
		 */
		else if (type.equals("listClass")) {
			String classid = request.getParameter("classid");
			js = qServiceLogic.getClasses(classid);
		}
		/**
		 * 显示咨询列表
		 */
		else if (type.equals("listNote")) {
			String querytype = request.getParameter("querytype");
			String searchKey = request.getParameter("searchKey");
			String max = request.getParameter("max");
			int max1 = Integer.parseInt(max == null || "".equals(max) ? "0"
					: max);
			String page = request.getParameter("page");
			int page1 = Integer.parseInt(page == null || "".equals(page) ? "0"
					: page);
			js = qServiceLogic.getNotes(querytype, searchKey, max1, page1);
		}
		/**
		 * 取得首页幻灯
		 */
		else if (type.equals("Slide")) {
			js = qServiceLogic.getSlide();
		}
		/**
		 * 取出疾病索引
		 */
		else if (type.equals("Sike")) {
			List list = null;
			for (char item = 'A'; item <= 'Z'; item++) {
				if (list == null) {
					list = qServiceLogic.getSike(item);
				} else {
					list.addAll(qServiceLogic.getSike(item));
				}
				log.debug(item);
			}
			//过滤不需要的字段start
			JsonConfig cfg = new JsonConfig();
			cfg.setJsonPropertyFilter(new PropertyFilter() {
				public boolean apply(Object source, String name, Object value) {
					if (name.equals("ordernum")||name.equals("addtime")) {
						return true;
					} else {
						return false;
					}
				}
			});


			JSONArray ja = JSONArray.fromObject(list, cfg);
			//过滤字段结over
	
			
			js = ja.toString();
		}

		log.debug(js);
		out.println(jsoncallback + "(" + js + ")");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here

	}

}
